// 弹幕
const express = require('express');
const query = require('../utils/query');
const {check} = require('../utils/valitate');
const router = express.Router();

// 保存弹幕
router.post(
    '/',
    check([], ['video_id', 'text', 'color', 'time']),  // 参数校验
    async (req, res) => {
        // 获取请求体参数
        const {video_id, text, color, time} = req.body;
        // 获取发射弹幕用户的id
        const user_id = req.userInfoLaoGuo.Id;
        // 执行插入操作
        await query(
            'INSERT INTO barrage (video_id,text,color,time,user_id) VALUES (?,?,?,?,?)',
            [video_id, text, color, time, user_id]
        );
        res.send({
            code: 1,
            msg: '发射成功'
        });
    }
)
// 获取弹幕
router.get(
    '/',
    check(['id'], []),
    async function (req, res) {
        // 获取视频id和请求参数一致的所有弹幕
        const sql = `SELECT text,color,time FROM barrage WHERE video_id=?`;
        const data = await query(sql, [req.query.id]);
        res.send({
            code: 1,
            msg: '获取成功',
            data: data
        })
    }
);

module.exports = router;
